package com.cqupt.scanning.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @ClassName: GenerateCode
 * @Date: 2021/3/26 15:29
 * @Author: 宋宝梁
 */


public class GenerateCode {
    public static void main(String[] args) {
        //  获取 代码生成器对象
        AutoGenerator autoGenerator = new AutoGenerator();
        // 配置策略

        // 1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("宋宝梁");
        gc.setOpen(false);
        gc.setFileOverride(true); // 是否覆盖
        gc.setServiceName("%sService");
        gc.setIdType(IdType.AUTO);
        gc.setSwagger2(false); // 配置swagger
        autoGenerator.setGlobalConfig(gc);

        // 2.配置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/scanning?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("wangjie");
        dataSourceConfig.setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(dataSourceConfig);

        // 3.包的配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName("statistics");
        packageConfig.setParent("com.cqupt.scanning.system");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setController("controller");
        autoGenerator.setPackageInfo(packageConfig);

        // 4.策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("calculated_data_foot1"); // 设置要映射的表
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true); // 配置lombok
        strategy.setLogicDeleteFieldName("deleted"); // 配置逻辑删除
        // 自动填充配置
        TableFill creatTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        List<TableFill> list = new ArrayList<>();
        list.add(creatTime);
        list.add(updateTime);
        strategy.setTableFillList(list);

        strategy.setVersionFieldName("version"); // 配置乐观锁
        strategy.setRestControllerStyle(true); // 配置restful风格
        strategy.setControllerMappingHyphenStyle(true); // 配置url采用下划线的风格
        autoGenerator.setStrategy(strategy);

        // 执行
        autoGenerator.execute();
    }
}
